package com.future.baselib.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.R;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.utils.FLog;
import com.future.baselib.utils.ToastUtils;
import com.future.baselib.view.LoadingDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Fragment基类
 * <p>Created by kam on 2016/6/25.
 */
public abstract class BaseFragment extends Fragment {
    public Context mContext;
    public LayoutInflater mInflater;

    protected FrameLayout titleBar;
    protected TextView title;
    protected TextView tvTitleLeft;
    protected ImageView ivTitleLeft;
    protected TextView tvTitleRight;
    protected ImageView ivTitleRight;

    private LoadingDialog dialog;

    protected ToastUtils toast;

    /**
     * Bundle  传递数据
     *
     * @param extras
     */
    protected abstract void getBundleExtras(Bundle extras);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mInflater = LayoutInflater.from(mContext);
        Bundle extras = getArguments();
        if (extras != null) {
            getBundleExtras(extras);
        }
        toast = new ToastUtils(mContext);

        //此处可以给Fragment设置主题
        //mContext = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);

        if (isRegisterEventBus()){
            EventBusUtil.register(this);
        }
    }

    @Nullable
    @LayoutRes
    protected abstract int getLayoutResId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResId(), container, false);
        titleBar = (FrameLayout) view.findViewById(R.id.layout_title);
        if (titleBar != null) {
            initTitle(view);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view,savedInstanceState);
    }

    /**
     * 初始化view
     */
    protected abstract void initView(View view,@Nullable Bundle savedInstanceState);

    public void onMsgObtain(Message msg) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(MessageEvent event) {}

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(MessageEvent event) {}

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }



    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            onMsgObtain(msg);
        }
    };

    /**
     * @return the handler
     */
    public Handler getHandler() {
        return handler;
    }

    public void showShortToast(String text) {
        ToastUtils.shortToast(mContext, text);
    }

    public void showLongToast(String text) {
        ToastUtils.longToast(mContext, text);
    }

    /**
     * 跳转
     *
     * @param activityClass
     */
    public void startActivity(Class activityClass) {
        startActivity(activityClass, null);
    }

    public void startActivity(Class activityClass, Bundle bundle) {
        Intent intent = new Intent(mContext, activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivityForResult(Class activityClass, int requestCode) {
        startActivityForResult(activityClass, null, requestCode);
    }

    public void startActivityForResult(Class activityClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(mContext, activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 加载对话框
     */
    public void showLoadingDialog() {
        if (dialog == null) {
            dialog = new LoadingDialog(mContext);
        }
        dialog.show();
    }

    public void dissmissLoadingDialog() {
        if (dialog != null) {
            dialog.close();
        }
    }

    protected void initTitle(View view) {
        title = (TextView) view.findViewById(R.id.tv_title);
        ivTitleLeft = (ImageView) view.findViewById(R.id.iv_left);
        ivTitleRight = (ImageView) view.findViewById(R.id.iv_right);
        tvTitleRight = (TextView) view.findViewById(R.id.tv_right);
        tvTitleLeft = (TextView) view.findViewById(R.id.tv_left);
    }

    public void setTitleLeft(int drawable, View.OnClickListener listener) {
        ivTitleLeft.setImageResource(drawable);
        ivTitleLeft.setOnClickListener(listener);
        ivTitleLeft.setVisibility(View.VISIBLE);
        tvTitleLeft.setVisibility(View.GONE);
    }

    public void setTitleRight(int drawable, View.OnClickListener listener) {
        ivTitleRight.setImageResource(drawable);
        ivTitleRight.setOnClickListener(listener);
        ivTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setVisibility(View.GONE);
    }

    public void setTitleRight(String str, View.OnClickListener listener) {
        tvTitleRight.setText(str);
        tvTitleRight.setOnClickListener(listener);
        tvTitleRight.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
    }

    public void setTitleLeft(String str, View.OnClickListener listener){
        tvTitleLeft.setText(str);
        tvTitleLeft.setOnClickListener(listener);
        tvTitleLeft.setVisibility(View.VISIBLE);
        ivTitleLeft.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        if (null != this.title)
            this.title.setText(title);
    }

    public void setTitle(int resId) {
        if (null != this.title) {
            this.title.setText(resId);
        }
    }

    public void toast(String msg) {
        toast.show(msg);
    }

    public void loge(String tag, String msg) {
        FLog.e(msg);
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onStop() {
        super.onStop();
        toast.cancel();
        if (isRegisterEventBus()){
            EventBusUtil.unregister(this);
        }
    }


}